package pl.edu.uj.tcs.blef.protocol;

import pl.edu.uj.tcs.blef.utils.Ace;
import pl.edu.uj.tcs.blef.utils.Bet;
import pl.edu.uj.tcs.blef.utils.Card;
import pl.edu.uj.tcs.blef.utils.Clubs;
import pl.edu.uj.tcs.blef.utils.Color;
import pl.edu.uj.tcs.blef.utils.ColorBet;
import pl.edu.uj.tcs.blef.utils.Diamonds;
import pl.edu.uj.tcs.blef.utils.Four;
import pl.edu.uj.tcs.blef.utils.Full;
import pl.edu.uj.tcs.blef.utils.Hearts;
import pl.edu.uj.tcs.blef.utils.HighCard;
import pl.edu.uj.tcs.blef.utils.Jack;
import pl.edu.uj.tcs.blef.utils.King;
import pl.edu.uj.tcs.blef.utils.Nine;
import pl.edu.uj.tcs.blef.utils.Pair;
import pl.edu.uj.tcs.blef.utils.Queen;
import pl.edu.uj.tcs.blef.utils.RoyalFlush;
import pl.edu.uj.tcs.blef.utils.Spades;
import pl.edu.uj.tcs.blef.utils.StraightFlush;
import pl.edu.uj.tcs.blef.utils.Ten;
import pl.edu.uj.tcs.blef.utils.Three;
import pl.edu.uj.tcs.blef.utils.TwoPairs;

public class BetRequestFactory implements RequestFactory {

	@Override
	public Request build(Object[] parameters, RequestHandler handler) throws BuildFailedException {
		if(parameters.length != 1) throw new BuildFailedException("One argument expected.");
		try{
			Bet bet = (Bet)parameters[0];
			return new BetRequest("/bet "+bet.getCode(), handler);
		} catch(ClassCastException e) { throw new BuildFailedException(e.getMessage()); }
	}

	@Override
	public Request parse(String message, RequestHandler handler) throws ParseFailedException, BuildFailedException {
		if(message.split(" ").length != 3 && message.split(" ").length != 4) throw new ParseFailedException("Three or four arguments expected.");
		if("/bet".equals(message.split(" ")[0])){
			Object tab[] = new Object[1];
			Color color = null; Card first = null; Card second = null;
			if("C".equals(message.split(" ")[2])) color = new Clubs();
			else if("D".equals(message.split(" ")[2])) color = new Diamonds();
			else if("H".equals(message.split(" ")[2])) color = new Hearts();
			else if("S".equals(message.split(" ")[2])) color = new Spades();
			
			if("9".equals(message.split(" ")[2])) first = new Nine(color);
			else if("10".equals(message.split(" ")[2])) first = new Ten(color);
			else if("J".equals(message.split(" ")[2])) first = new Jack(color);
			else if("Q".equals(message.split(" ")[2])) first = new Queen(color);
			else if("K".equals(message.split(" ")[2])) first = new King(color);
			else if("A".equals(message.split(" ")[2])) first = new Ace(color);
			
			if(message.split(" ").length == 4){
				if("9".equals(message.split(" ")[3])) second = new Nine(color);
				else if("10".equals(message.split(" ")[3])) second = new Ten(color);
				else if("J".equals(message.split(" ")[3])) second = new Jack(color);
				else if("Q".equals(message.split(" ")[3])) second = new Queen(color);
				else if("K".equals(message.split(" ")[3])) second = new King(color);
				else if("A".equals(message.split(" ")[3])) second = new Ace(color);
			}
			
			if("ONE".equals(message.split(" ")[1])) tab[0] = new HighCard(first);
			else if("PAIR".equals(message.split(" ")[1])) tab[0] = new Pair(first);
			else if("TWO-PAIRS".equals(message.split(" ")[1])) tab[0] = new TwoPairs(first, second);
			else if("THREE".equals(message.split(" ")[1])) tab[0] = new Three(first);
			else if("FULL".equals(message.split(" ")[1])) tab[0] = new Full(first, second);
			else if("COLOR".equals(message.split(" ")[1])) tab[0] = new ColorBet(color);
			else if("FOUR".equals(message.split(" ")[1])) tab[0] = new Four(first);
			else if("STRAIGHT-FLUSH".equals(message.split(" ")[1])) tab[0] = new StraightFlush(color);
			else if("ROYAL-FLUSH".equals(message.split(" ")[1])) tab[0] = new RoyalFlush(color);
			return build(tab, handler);
		}
		else throw new ParseFailedException("Unknown request.");
	}

}
